package com.wedo.mall.activity.user;

import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.LinearLayout;

import com.e.library.activity.EActivity;
import com.e.library.http.EApi;
import com.e.library.http.EClient;
import com.e.library.http.EObserver;
import com.e.library.http.EResponse;
import com.e.library.utils.ESPUtils;
import com.wedo.mall.R;
import com.wedo.mall.entity.Article;
import com.wedo.mall.widget.EWebView;

import java.util.HashMap;
import java.util.Locale;

import butterknife.BindView;
import butterknife.OnClick;
import cn.sharesdk.facebook.Facebook;
import cn.sharesdk.framework.Platform;
import cn.sharesdk.framework.PlatformActionListener;
import cn.sharesdk.onekeyshare.OnekeyShare;
import cn.sharesdk.wechat.friends.Wechat;
import cn.sharesdk.wechat.moments.WechatMoments;
import cn.sharesdk.whatsapp.WhatsApp;
import io.reactivex.Observable;

public class RecommendPrizeActivity extends EActivity {

    public static final String SHARE_LOGO = "/sdcard/com.wedo.mall/logo.png";
    private OnekeyShare oks = new OnekeyShare();

    @BindView(R.id.ll_web) LinearLayout ll_web;
    private EWebView eWebView;

    @Override
    protected int layout() {
        return R.layout.activity_recommend_prize;
    }

    @Override
    protected void onActivityCreated(Bundle state) {
        eWebView = new EWebView(ll_web);
        article();

        oks.disableSSOWhenAuthorize();
        oks.setUrl(EApi.BASE_URL);
        oks.setSilent(true);
        oks.setImagePath(SHARE_LOGO);
        oks.setText(getString(R.string.user_wedobest));

    }

    @Override
    protected void onPause() {
        super.onPause();
        isCorE();
    }

    private void article(){
        loading();
        Observable<EResponse> observable = api().article(Article.SIGN_SHARE);
        observer(observable, new EObserver() {
            @Override
            public void successful(EResponse response) {
                dismiss();
                Article article = response.object("list",Article.class);
                if(article == null || TextUtils.isEmpty(article.content)) {
                    toast(getString(R.string.user_dataanomaly));
                    return;
                }
                eWebView.data(article.content);
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }
        });
    }

    /**
     * 推荐有奖分享成功回调
     */
    private void shareCallback(){
        loading();
        EClient.with().post(EApi.USER_SHARECALLBACK, new EObserver() {
            @Override
            public void successful(EResponse response) {

                dismiss();
            }

            @Override
            public void failure(String msg) {
                dismiss(msg);
            }

        });
    }

    @Override
    protected void onDestroy() {
        eWebView.onDestroy();
        super.onDestroy();
    }

    @OnClick(R.id.rl_detail)
    protected void onDetailClick(){
        toActivity(RecommendPrizeDetailActivity.class);
    }

    @OnClick({R.id.iv_share_wechat, R.id.iv_share_pyq, R.id.iv_share_facebook, R.id.iv_share_whatsapp})
    protected void onShareClick(View view) {
        switch (view.getId()) {
            case R.id.iv_share_wechat:
                oks.setPlatform(Wechat.NAME);
                break;
            case R.id.iv_share_pyq:
                oks.setPlatform(WechatMoments.NAME);
                break;
            case R.id.iv_share_facebook:
                oks.setPlatform(Facebook.NAME);
                break;
            case R.id.iv_share_whatsapp:
                oks.setPlatform(WhatsApp.NAME);
                break;
        }
        oks.setCallback(new OneKeyShareCallback());
/*        Platform pf = ShareSDK.getPlatform(WhatsApp.NAME);
        pf.setPlatformActionListener(new PlatformActionListener() {
            @Override
            public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
                shareCallback();
            }

            @Override
            public void onError(Platform platform, int i, Throwable throwable) {

            }

            @Override
            public void onCancel(Platform platform, int i) {

            }
        });*/
        oks.show(this);
        dismiss();
    }

    private class OneKeyShareCallback implements PlatformActionListener {
        @Override
        public void onComplete(Platform platform, int i, HashMap<String, Object> hashMap) {
            shareCallback();
        }

        @Override
        public void onError(Platform platform, int i, Throwable throwable) {

        }

        @Override
        public void onCancel(Platform platform, int i) {

        }
    }


}
